package com.Page.exam.dao;

import java.sql.Types;
import java.util.List;
import java.util.UUID;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.Page.common.PageData;
import com.Page.common.uitl.PageJdbcUtil;
import com.Page.exam.dto.QueryTestDomain;
import com.Page.exam.dto.TestDTO;
/**
 * 
* Copyright: Copyright (c) 2019 YancyChong.All rights reserved.
* 
* @ClassName: TestDAO.java
* @Description: 该类的功能描述
*
* @version: v1.0.0
* @author: YancyChong
* @date: 2019年5月15日 下午8:53:18 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2019年5月15日     User           v1.0.0               修改原因
 */
@Component
public class TestquestionDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
	@Autowired
	private PageJdbcUtil pageJdbcUtil;
	
	//添加题目表信息
	public TestDTO add(TestDTO dto) {
        final String sql = "INSERT into  question (id, content, answer, konwledge_area) VALUES (?, ?, ?, ?)";
        dto.setId(UUID.randomUUID().toString());//生成ID
        jdbcTemplate.update(sql,  
                new Object[] {dto.getId(), dto.getContent(), dto.getAnswer(), dto.getArea()},
                new int[] {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR}
                );
        return dto;
    }
	
	/**
	* @Description:查询
	*
	* @param:描述1描述
	* @return：返回结果描述
	* @throws：异常描述
	*
	* @version: v1.0.0
	* @author: YancyChong
	* @date: 2019年5月15日 下午8:53:46 
	*
	* Modification History:
	* Date         Author          Version            Description
	*---------------------------------------------------------*
	* 2019年5月15日     User           v1.0.0               修改原因
	 */
	public TestDTO add1(TestDTO dto) {
        final String sql = "insert into  question (id, content, answer, konwledge_area) VALUES (?, ?, ?, ?)";
        dto.setId(UUID.randomUUID().toString());
        jdbcTemplate.update(sql,  
                dto.getId(), dto.getContent(), dto.getAnswer(), dto.getArea() );
        return dto;
    }
	
	
	
	
	
	
	public TestDTO findById (String id) {
		final String sql = "select id, content, answer, konwledge_area  from  question where id = ?";
		List<TestDTO> list = jdbcTemplate.query(sql, 
                new Object[] {id},
                new int[] {Types.VARCHAR},
				(ResultSet rs, int rowNum) -> new TestDTO (rs.getString("id"), rs.getString("content"), rs.getString("answer"), rs.getString("konwledge_area"))
				);
		if (null != list && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	/**
	* @Description:  jdbcTemplate.query的另一种实现方式
	*
	* @param:描述1描述
	* @return：返回结果描述
	* @throws：异常描述
	*
	* @version: v1.0.0
	* @author: YancyChong
	* @date: 2019年5月15日 下午9:12:18 
	*
	* Modification History:
	* Date         Author          Version            Description
	*---------------------------------------------------------*
	* 2019年5月15日     User           v1.0.0               修改原因
	 */
	public TestDTO findById1 (String id) {
		final String sql = "SELECT ID, CONTENT, ANSWER,KNOWLEDGE_AREA  FROM  question WHERE ID = ?";
		List<TestDTO> list = jdbcTemplate.query(sql, new Object[]{id}, new RowMapper<TestDTO>() {
	            public TestDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
	            	TestDTO test = new TestDTO();
	            	test.setId(rs.getString("id"));
	            	test.setContent(rs.getString("content"));
	            	test.setAnswer(rs.getString("answer"));
	            	test.setArea(rs.getString("konwledge_area"));
	                return test;
	            }
	        });

		if (null != list && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	
	RowMapper<TestDTO> testRM = new RowMapper<TestDTO>() {

		@Override
		public TestDTO mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			// TODO Auto-generated method stub
			TestDTO test = new TestDTO();
			test.setId(rs.getString("id"));
        	test.setContent(rs.getString("content"));
        	test.setAnswer(rs.getString("answer"));
        	test.setArea(rs.getString("konwledge_area"));
			return test;
		}
	};

	public PageData<TestDTO> getTestPage(
			QueryTestDomain domain) {

		String querySql = "select id, content, answer,konwledge_area  from  `question` ";
		String countSql = "select count(1) from `question` ";
		
		return pageJdbcUtil.getPageData(domain.getPageSize(),
				domain.getPageNumber(), testRM, querySql, countSql);
	}
	
	//覆盖题目
	public TestDTO update(TestDTO dto) {
        final String sql = "UPDATE  `question` SET CONTENT = ?, ANSWER = ?, KONWLEDGE_AREA = ? WHERE ID = ?";
        jdbcTemplate.update(sql,  
                new Object[] {dto.getContent(), dto.getAnswer(), dto.getArea(), dto.getId()},
                new int[] {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR}
                );
        return dto;
    }

	//删除题目
	public int delete(String id) {
        final String sql = "DELETE FROM  `question` WHERE ID = ?";
        return jdbcTemplate.update(sql,  
                new Object[] {id},
                new int[] {Types.VARCHAR}
                );
    }
}
